-
-
Notifications
You must be signed in to change notification settings - Fork 411
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
setTimeout
, setInterval
and clearInterval
(and the same clearTimeout
) implementations
#4130
base: main
Are you sure you want to change the base?
Conversation
…imeout`) implementations This adds non-async implementations of the `setTimeout`/`setInterval` API functions. They are by default registered in the context when registering all APIs from `boa_runtime`.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really nice implementation! I have some opinions on using SystemTime
, but aside from that everything else looks great!
/// Create a new `TimeoutJob` with a timeout and a job. | ||
#[must_use] | ||
pub fn new_unchecked(job: NativeJob, timeout: i64) -> Self { | ||
Self { timeout, job } | ||
} | ||
|
||
/// Create a new `TimeoutJob` with a job and a timeout in milliseconds in the future. | ||
#[must_use] | ||
pub fn delayed(job: NativeJob, delay: u64, context: &mut Context) -> Self { | ||
Self::new_unchecked(job, context.host_hooks().utc_now() + (delay as i64)) | ||
} | ||
|
||
/// Creates a new `TimeoutJob` from a closure and a timeout as [`std::time::SystemTime`]. | ||
#[must_use] | ||
pub fn new<F>(f: F, timeout: std::time::SystemTime) -> Self | ||
where | ||
F: FnOnce(&mut Context) -> JsResult<JsValue> + 'static, | ||
{ | ||
Self::new_unchecked( | ||
NativeJob::new(f), | ||
timeout | ||
.duration_since(std::time::UNIX_EPOCH) | ||
.expect("Invalid SystemTime") | ||
.as_millis() as i64, | ||
) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we shouldn't depend so heavily on SystemTime
. Seems weird to tie one of our job types to something that could panic on certain platforms, and users won't be able to sandbox the engine from the OS if this is in place.
Having new(job: NativeJob, timeout: i64)
sounds enough for all usecases, and the specific executor can use the provided timeout to schedule the job as it fits.
/// The instant this job should be run, in msec since epoch. This will be compared | ||
/// to the host's [`HostHooks::utc_now`] method. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we should mention details about SimpleJobExecutor
, since every executor can handle the timeout amount in a different way.
This adds non-async implementations of the
setTimeout
/setInterval
API functions. They are by default registered in the context when registering all APIs fromboa_runtime
.This adds a new type of jobs that implement
HostEnqueueTimeoutJob
(see https://tc39.es/ecma262/#sec-hostenqueuetimeoutjob).This requires #4141 to be merged first.